package com.es.spring_es.services;

import com.es.spring_es.clientmap.EsUtils;
import com.es.spring_es.model.DriverLocationEntity;
import com.es.spring_es.model.DriverLocationMainEntity;
import com.es.spring_es.model.params.ReturnParams;
import com.es.spring_es.model.vo.Point;
import com.es.spring_es.model.vo.PointAndTime;
import com.es.spring_es.model.vo.SearchResult;
import com.es.spring_es.model.vo.Track;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.klock.annotation.Klock;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xu
 * @date 2020/6/3
 * @desciption 整合层
 */
@Service
public class EsServices {

    @Autowired
    GaodeServices gaodeServices;

    @Autowired
    DriverlocationMainServices driverlocationMainServices;

    @Autowired
    DriverlocationServices driverlocationServices;



    private static Logger logger = LogManager.getLogger(EsServices.class);

    /**
     * 上传司机轨迹到es服务器
     * @return
     */
    public ReturnParams uploadDriverLocation(String tid,Long trid){

        try {
            //String tid, String trid, boolean correction, Integer page, Integer pageSize
            SearchResult trsearch = gaodeServices.trsearch(tid, trid.toString(), false, 1, 3);
            Integer counts = trsearch.getCounts();

            DriverLocationMainEntity driverLocationMainEntity = new DriverLocationMainEntity();
            driverLocationMainEntity.setCounts((long)counts);
            driverLocationMainEntity.setDistance((long)trsearch.getTracks().get(0).getDistance());
            driverLocationMainEntity.setEndPoint_locateTime(trsearch.getTracks().get(0).getEndPoint().getLocatetime());
            driverLocationMainEntity.setEndPoint_location(trsearch.getTracks().get(0).getEndPoint().getLocation());
            driverLocationMainEntity.setStartPoint_locateTime(trsearch.getTracks().get(0).getStartPoint().getLocatetime());
            driverLocationMainEntity.setStartPoint_location(trsearch.getTracks().get(0).getStartPoint().getLocation());
            driverLocationMainEntity.setTime((long)trsearch.getTracks().get(0).getTime());
            driverLocationMainEntity.setTrid(trid);

            int pagesize=200;
            ArrayList<DriverLocationEntity> points = new ArrayList<DriverLocationEntity>();
            int totalPage = (counts + pagesize -1) / pagesize;
            for (int i=1;i<=totalPage;i++){
                try {
                    SearchResult result = gaodeServices.trsearch( tid, trid.toString(), false, i, pagesize);
                    Thread.sleep(2000);
                    ArrayList<Point> ps = result.getTracks().get(0).getPoints();
                    int size=  points.size();
                   // points.addAll(ps);
                    for(int j = points.size()+1;j < ps.size() + size+1;j++){
                        DriverLocationEntity driverLocationEntity = new DriverLocationEntity();
                        //driverLocationEntity.setDl_id((long)j);
                        driverLocationEntity.setSpeed(ps.get(j-points.size()-1).getSpeed());
                        driverLocationEntity.setLocatetime(ps.get(j-points.size()-1).getLocatetime());
                        driverLocationEntity.setHeight(ps.get(j-points.size()-1).getHeight());
                        driverLocationEntity.setDirection(ps.get(j-points.size()-1).getDirection());
                        driverLocationEntity.setTrid(trid);
                        driverLocationEntity.setLocation(ps.get(j-points.size()-1).getLocation());
                        driverLocationEntity.setAccuracy(ps.get(j-points.size()-1).getAccuracy());

                        points.add(driverLocationEntity);

                    }

                    //数据存储es服务
                    //轨迹点数据
                    driverlocationServices.addDriverData(points);

                    //主类数据
                    driverlocationMainServices.addDriverLocationMainData(driverLocationMainEntity);

                } catch (Throwable e) {
                    logger.error("获取轨迹失败，trid:  "+trid,e);
                }
            }

            return new ReturnParams(0,"success","success");
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnParams(1,"error",e.getMessage());
        }

    }


    /**
     * 获取高德数据列表
     */
    public ReturnParams getDriverLocationList(String tid,Long trid){
        try {

           //查询主类信息
            List<DriverLocationMainEntity> driverLocationMainByTrid = driverlocationMainServices.getDriverLocationMainByTrid(trid + "");


            //查询点信息
            List<DriverLocationEntity> driverLocationEntities = driverlocationServices.searchDriverLocalByTrid(trid + "");

            //拼接返回结果
            SearchResult result = new SearchResult();
            if(driverLocationMainByTrid != null && driverLocationMainByTrid.size() > 0){
                Track t = new Track();
                t.setTrid(trid.intValue());
                t.setDistance(driverLocationMainByTrid.get(0).getDistance().intValue());
                PointAndTime  end = new PointAndTime();
                end.setLocatetime(driverLocationMainByTrid.get(0).getEndPoint_locateTime());
                end.setLocation(driverLocationMainByTrid.get(0).getEndPoint_location());
                t.setEndPoint(end);

                PointAndTime  start = new PointAndTime();
                start.setLocatetime(driverLocationMainByTrid.get(0).getStartPoint_locateTime());
                start.setLocation(driverLocationMainByTrid.get(0).getStartPoint_location());
                t.setStartPoint(start);

                t.setCounts(driverLocationEntities.size());

                ArrayList<Point> li = new ArrayList<>();
                //转换point点数据
                for(int i = 0;i < driverLocationEntities.size(); i++){
                    Point p = new Point();
                    p.setAccuracy(driverLocationEntities.get(i).getAccuracy());
                    p.setDirection(driverLocationEntities.get(i).getDirection());
                    p.setHeight(driverLocationEntities.get(i).getHeight());
                    p.setLocatetime(driverLocationEntities.get(i).getLocatetime());
                    p.setLocation(driverLocationEntities.get(i).getLocation());
                    p.setSpeed(driverLocationEntities.get(i).getSpeed());
                    li.add(p);
                }

                t.setPoints(li);

                ArrayList<Track> tracks = new ArrayList<>();
                tracks.add(t);
                result.setTracks(tracks);
                result.setCounts(1);
            }


             return new ReturnParams(0,"success",result);

        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnParams(1,"error",null);
        }


    }


    /**
     * test
     */
    @Klock(waitTime = Long.MAX_VALUE)
    public ReturnParams test(String tid,Long trid){



      return null;
    }


}
